/***************************************************************************************
* Copyright (c) 2014-2022 Zihao Yu, Nanjing University
*
* NEMU is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*          http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
***************************************************************************************/

#include <isa.h>
#include <cpu/difftest.h>
#include <stdio.h>
#include "../local-include/reg.h"

extern const char *regs[32];
extern unsigned int inst_num;

void rb_display();

bool isa_difftest_checkregs(CPU_state *ref_r, vaddr_t pc) {
  bool ret = true;

  // printf("ref:next pc is %#x\n", ref_r->pc);
  // for (int i = 0; i < 32; i++) {
  //   printf("x%d  %#x\n", i, ref_r->gpr[i]);
  // }

  if (ref_r->pc != cpu.pc) {
    printf("Have executed %u instruction(s)\n", inst_num);
    printf("PC:0x%08x is not same as ref:0x%08x\n", cpu.pc, ref_r->pc);
    ret = false;
  }

  int num = MUXDEF(CONFIG_RVE, 16, 32);
  for (int i = 0; i < num && ret; i ++) {
    if (ref_r->gpr[i] != cpu.gpr[i]) {
      printf("Have executed %u instruction(s)\n", inst_num);
      printf("Register %s is not same as ref:0x%08x\n", regs[i], ref_r->gpr[i]);
      ret = false;
      break;
    }
  }

  if (!ret) {
    printf("At 0x%08x!!!\n", pc);
  }

  return ret;
}

void isa_difftest_attach() {
}
